//
//  SampleDetailView.m
//  GoldOperationClient
//
//  Created by yaxue wang on 2016/12/20.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "SampleDetailView.h"
@interface SampleDetailView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) NSMutableArray *dataAr;
@property(nonatomic,strong) NSString *total_price;
@property(nonatomic,strong)UILabel *totalLab;
@property(nonatomic)double totalPrice;
@end
@implementation SampleDetailView


-(instancetype)initWithFrame:(CGRect)frame withWarningUuid:(NSString *)warningUuid
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataAr=[[NSMutableArray alloc]init];
        _totalPrice=0.00;
        self.backgroundColor =C(@"f0f0f0");
        self.delegate=self;
        self.dataSource=self;
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator =NO;
        //1 - User端已确认采购单申请  2 - User端拒绝采购单申请
        [CClass HttpRequestTask:YES withUrl:@"purchase_listAllFlatten" withData:@{@"warning_uuid":warningUuid,@"state":@1} notifyError:YES onSuccess:^(BaseModel *model) {

            NSMutableArray *ar=[[NSMutableArray alloc]init];
            ar =[model.B_data objectForKey:@"list"];
            for (int i=0; i<ar.count; i++) {
                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                dic=[ar objectAtIndex:i];
            NSMutableArray *smallAr=[[NSMutableArray alloc]init];
            if ([F_NSpace([dic objectForKey:@"created"]) isEqualToString:@""]) {
                
            }else
            {
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:@"确认时间"forKey:@"place"];
                NSString *timeString = [[dic objectForKey:@"created"] substringWithRange:NSMakeRange(0, 16)];
                [dataDic setObject:timeString forKey:@"name"];
                [smallAr addObject:dataDic];
                
            }
            if ([F_NSpace([dic objectForKey:@"name"]) isEqualToString:@""]) {
                
            }else
            {
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:@"物品名称"forKey:@"place"];
                [dataDic setObject:[dic objectForKey:@"name"]forKey:@"name"];
                [smallAr addObject:dataDic];
                
            }
            if ([F_NSpace([dic objectForKey:@"spec"]) isEqualToString:@""]) {
                
            }else
            {
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:@"规格"forKey:@"place"];
                [dataDic setObject:[dic objectForKey:@"spec"]forKey:@"name"];
                [smallAr addObject:dataDic];
                
            }
            NSMutableDictionary *numDic=[[NSMutableDictionary alloc]init];
            [numDic setObject:@"数量"forKey:@"place"];
            [numDic setObject:[NSString stringWithFormat:@"%@",[dic objectForKey:@"count"]]forKey:@"name"];
            [smallAr addObject:numDic];
            
            NSMutableDictionary *totalDic=[[NSMutableDictionary alloc]init];
            [totalDic setObject:@"总价(元)"forKey:@"place"];
            double totalDouble=[[NSString stringWithFormat:@"%@",[dic objectForKey:@"total_price"]] doubleValue];
            [totalDic setObject:[NSString stringWithFormat:@"%.2f",totalDouble]forKey:@"name"];
            [smallAr addObject:totalDic];
            _totalPrice +=[[dic objectForKey:@"total_price"] doubleValue];
            
            if ([F_NSpace([dic objectForKey:@"purpose"]) isEqualToString:@""]) {
                
            }else
            {
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:@"用途"forKey:@"place"];
                [dataDic setObject:[dic objectForKey:@"purpose"]forKey:@"name"];
                [smallAr addObject:dataDic];
                
            }
            if ([F_NSpace([dic objectForKey:@"remark"]) isEqualToString:@""]) {
                
            }else
            {
                NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
                [dataDic setObject:@"备注"forKey:@"place"];
                [dataDic setObject:[dic objectForKey:@"remark"]forKey:@"name"];
                [smallAr addObject:dataDic];
                
            }
            [_dataAr addObject:smallAr];
            
        
            }
            self.tableFooterView=[self footTableView];
            _totalLab.text=[NSString stringWithFormat:@"合计（元）：%.2f",_totalPrice];
            [self reloadData];
        } onError:^(BaseModel *model) {
            
        }];
       
        
    }
    
    return self;
    
}

//去掉粘合性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView ==self)
    {
        CGFloat sectionHeaderHeight = 40;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *ar=[self.dataAr objectAtIndex:section];
    return ar.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 70, 44)];
        titleLab.tag=100;
        titleLab.font=F(13);
        titleLab.textColor=C(@"a1a1a1");
        [cell.contentView addSubview:titleLab];
        
        UILabel *textLab =[[UILabel alloc]initWithFrame:CGRectMake(85+18, 0, WindowWidth-85-18-15, 44)];
        textLab.font =F(13);
        textLab.tag =200;
        textLab.numberOfLines=0;
        textLab.textColor=C(@"666666");
        [cell.contentView addSubview:textLab];
        
        UILabel *line1 =[[UILabel alloc]initWithFrame:CGRectMake(0,44-0.5, WindowWidth, 0.5)];
        line1.tag =400;
        line1.backgroundColor=LineColor;
        [cell.contentView addSubview:line1];
        
        
    }
    UILabel *nameLab=[cell viewWithTag:100];
    UILabel *textLab =[cell viewWithTag:200];
    NSMutableArray *smallAr=[_dataAr objectAtIndex:indexPath.section];
    NSMutableDictionary *dic=[smallAr objectAtIndex:indexPath.row];

    nameLab.text=[dic objectForKey:@"place"];
    textLab.text=[dic objectForKey:@"name"];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 50;
    
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 50)];
    headView.backgroundColor=C(@"f0f0f0");
    UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, WindowWidth-30, 50)];
    titleLab.text=[NSString stringWithFormat:@"明细（%d）",section+1];
    titleLab.font=F(14);
    titleLab.textColor=C(@"7d7d7d");
    [headView addSubview:titleLab];
    return headView;
    
}
- (UIView*)footTableView
{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 35+10+50)];
    view.backgroundColor =C(@"f0f0f0");
    _totalLab=[[UILabel alloc]initWithFrame:CGRectMake(15,0, WindowWidth-15, 35)];
    _totalLab.textColor=MainColor;
    _totalLab.font=F(14);
    _totalLab.text=@"合计（元）：0.00";
    [view addSubview:_totalLab];
    return view;
    
}



@end
